package com.curry.orangelife.mobile.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.jump.PropertyJumper;
import com.common.sp.PaySp;
import com.common.util.OrangePhoneUtil;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.JumpManager;
import com.orangelife.mobile.hourlyEmployee.activity.OrderDetailActivity;
import com.orangelife.mobile.individual.activity.BalanceDetailActivity;
import com.orangelife.mobile.goodsOrder.activity.MyGoodsOrderListActivity;
import com.orangelife.mobile.pay.interfaze.IWeChatCallBack;
import com.orangelife.mobile.pay.biz.PaymentModule;
import com.orangelife.mobile.lookRepair.activity.GrapSingle2Activity;
import com.orangelife.mobile.lookRepair.activity.LookForRepairDetailActivity;
import com.orangelife.mobile.parkingcard.activity.HaveCardListActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingCardFeeMineActivity;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartBiz;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/16.
 */
public class WXPayEntryActivity extends OrangeLifeBaseActivity implements IWXAPIEventHandler, View.OnClickListener {

    private IWXAPI api;

    private static IWeChatCallBack iWeChatCallBack;
    private Button btnPaidLeft;
    private Button btnPaidRight;
    private TextView tvTitle, tvInfo, tvPhone, tvOrderInfoText,
            tvOrderTotalPrice, tvValidityDateValue;
    private LinearLayout llReturn;
    private ImageView ivIcon;
    private String type;
    private RelativeLayout rlValidityDate;

    /**Activit 名称**/
    final  String MODULE_NAME="/payOdr/payResult";

    public void callBack(IWeChatCallBack iWeChatCallBack) {
        this.iWeChatCallBack = iWeChatCallBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paid_result);
        initView();
        api = WXAPIFactory.createWXAPI(this, PaySp.getWxAppID());
        api.handleIntent(getIntent(), this);
    }

    private void setShowText() {
        tvInfo.setText(PaySp.getInfo());
        btnPaidRight.setText(PaySp.getBtnText());
    }

    private void setShowOrder() {
        type = PaySp.getType();
        tvOrderInfoText.setText(PaySp.getOrderInfo());
        tvOrderTotalPrice.setText(PaySp.getTotalPrice());
        if (!PaySp.isSuccess()) {
            /** 支付失败 */
            ivIcon.setBackgroundResource(R.drawable.paid_fail);
            btnPaidLeft.setText(getString(R.string.paid_again));
            btnPaidRight.setText(getString(R.string.close));
        } else {
            /** 停车卡*/
            if (type.equals(PaymentModule.mode[5])) {
                rlValidityDate.setVisibility(View.VISIBLE);
                tvValidityDateValue.setText(CurryApplication.getInstance().getValidity());
            }
            /** 钱包余额充值 */
            if (type.equals(PaymentModule.mode[9])) {
                btnPaidLeft.setVisibility(View.GONE);
                ViewGroup.LayoutParams params = btnPaidRight.getLayoutParams();
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                btnPaidRight.setLayoutParams(params);
            }
        }
    }

    private void initView() {
        llReturn = (LinearLayout) findViewById(R.id.ib_title_left);
        rlValidityDate = (RelativeLayout) findViewById(R.id.rlValidityDate);
        ivIcon = (ImageView) findViewById(R.id.ivIcon);
        tvOrderInfoText = (TextView) findViewById(R.id.tvOrderInfoText);
        tvOrderTotalPrice = (TextView) findViewById(R.id.tvOrderTotalPrice);
        tvValidityDateValue = (TextView) findViewById(R.id.tvValidityDateValue);
        tvInfo = (TextView) findViewById(R.id.tvInfo);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        btnPaidRight = (Button) findViewById(R.id.btn_paid_home);
        btnPaidLeft = (Button) findViewById(R.id.btn_paid_detail);
        btnPaidLeft.setOnClickListener(this);
        btnPaidRight.setOnClickListener(this);
        tvPhone.setOnClickListener(this);
        tvTitle.setText(getResources().getString(R.string.paid_result));
        llReturn.setVisibility(View.GONE);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        iWeChatCallBack.getResp(resp);
        setShowText();
        setShowOrder();
    }

    /**
     * 数据采集体不要删除
     * @return
     */
    public Map<String,Object> getDataTrack(){
        Map<String,Object> map=new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        map.put("payResult",PaySp.isSuccess()==false?"失败":"成功");
        return map;
    }

    private void finishActivity() {
        ExitApplication.getInstance().addPaidActivity(
                WXPayEntryActivity.this);
        ExitApplication.getInstance().finishPaidActivity();
        ExitApplication.getInstance().finishFailActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_paid_detail:
                dataTrackClick("btnViewOdr",null);
                if (!PaySp.isSuccess()) {
                    /** 支付失败 */
                    finish();
                } else {
                    finishActivity();
                    /** 支付成功 */
                    int feeType = Integer.parseInt(PaySp.getFeeType());
                    if (feeType == 2) {
                        /** 维修下单 */
                        Intent intent = new Intent(WXPayEntryActivity.this,
                                GrapSingle2Activity.class);
                        intent.putExtra("followID",
                                PaySp.getRepairID());
                        startActivity(intent);
                    } else if (feeType == 0) {
                        /**估价单*/
                        Intent intent = new Intent(WXPayEntryActivity.this,
                                LookForRepairDetailActivity.class);
                        intent.putExtra("jumpuri", "null");
                        startActivity(intent);
                    } else if (feeType == 1) {
                        /**定价单*/
                        Intent intent = new Intent(WXPayEntryActivity.this,
                                LookForRepairDetailActivity.class);
                        intent.putExtra("jumpuri", "null");
                        startActivity(intent);
                    } else {
                        if (type.equals(PaymentModule.mode[4])) {/* 物业费 */
                            PropertyJumper.jumpToPropertyHistoryList(WXPayEntryActivity.this);
                        } else if (type.equals(PaymentModule.mode[0]) || type.equals(PaymentModule.mode[2])) {
                            /** 餐桌，团购，积分商品 */
                            Intent intent = new Intent(WXPayEntryActivity.this, MyGoodsOrderListActivity.class);
                            startActivity(intent);
                        } else if (type.equals(PaymentModule.mode[8])) {
                            /** 钟点工 */
                            Intent intent = new Intent(WXPayEntryActivity.this,
                                    OrderDetailActivity.class);
                            intent.putExtra("jumpuri",
                                    PaySp.getServiceOrderID());
                            startActivity(intent);
                        } else if (type.equals(PaymentModule.mode[5])) {
                            /**停车卡*/
                            Intent intent = new Intent(WXPayEntryActivity.this,
                                    ParkingCardFeeMineActivity.class);
                            startActivity(intent);
                        } else if (type.equals(PaymentModule.mode[9])) {
                            /**钱包余额充值*/
                            Intent intent = new Intent(WXPayEntryActivity.this,
                                    BalanceDetailActivity.class);
                            startActivity(intent);
                        } else if (type.equals(PaymentModule.mode[1])) {
                            Intent intent = new Intent(WXPayEntryActivity.this, MyGoodsOrderListActivity.class);
                            startActivity(intent);
                            /**删除购物车中已购买的商品*/
                            ShoppingCartBiz.delGoodsList(getProducts());
                        }
                    }
                }
                break;
            case R.id.btn_paid_home:
                ExitApplication.getInstance().addPaidActivity(
                        WXPayEntryActivity.this);
                if (type.equals(PaymentModule.mode[5])) {
                    Intent intent = new Intent(WXPayEntryActivity.this,
                            HaveCardListActivity.class);
                    startActivity(intent);
                } else if (type.equals(PaymentModule.mode[9])) {
                    finishActivity();
                    /**钱包余额充值*/
                    Intent intent = new Intent(WXPayEntryActivity.this,
                            BalanceDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("walletID", CurryApplication.getInstance().getWalletID());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else if (type.equals(PaymentModule.mode[1])) {
                    finishActivity();
                    ShoppingCartBiz.delGoodsList(getProducts());
                    Intent intent = new Intent(WXPayEntryActivity.this, MyGoodsOrderListActivity.class);
                    startActivity(intent);
                } else {
                    ExitApplication.getInstance().addPaidActivity(
                            WXPayEntryActivity.this);
                    ExitApplication.getInstance().finishPaidActivity();
                }
                dataTrackClick("btnCpl",null);
                break;
            case R.id.tv_phone:
                OrangePhoneUtil.callCustomerService2(WXPayEntryActivity.this);
                break;
            default:
                break;
        }
    }

    private ArrayList<String> getProducts() {
        ArrayList<String> list = new ArrayList<>();
        String[] productsList;
        String products = PaySp.getProductID().replace(" ", "");
        if (products.contains(",")) {
            products = products.replace("[", "");
            products = products.replace("]", "");
            products.replace(" ", "");
            productsList = products.split(",");
        } else {
            productsList = new String[1];
            productsList[0] = PaySp.getProductID();
            productsList[0] = productsList[0].replace("[", "");
            productsList[0] = productsList[0].replace("]", "");
        }
        for (int i = 0; i < productsList.length; i++) {
            list.add(productsList[i]);
        }
        return list;
    }
}
